﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Helpers;

namespace DataProcessModule.Protocol
{
    public class FireboxStatsReport : BaseInstruction
    {
        #region param

        private string HardDiskSequence;//  灾备存储编号	4	DWORD 唯一编号, 设备中获取

        private UInt16 CanHardDiskSpace;//灾备存储容量（CAN数据）	2	WORD 单位MB，设备中获取

        private int Can1State;//灾备存储CAN1采集状态	1	BYTE	1:正常，0:异常

        private int Can2State;//灾备存储CAN2采集状态	1	BYTE	1:正常，0:异常

        private int MCUState;//灾备存储MCU状态	1	BYTE	1:正常，0:异常

        private int Device485State;//灾备存储485状态	1	BYTE	1:正常，0:异常

        private int FireboxState;//防火盒状态	1	BYTE	1.正常 0:不存在

        private int FireboxCamState;//防火盒录像状态	1	BYTE	0:未录像。1：主码流存储中

        private UInt32 FireboxVedioSpace;//防火盒录像存储容量(录像）	4	DWORD 单位MB，设备中获取

        private int FireboxVersion;//防火盒版本号  1	BYTE 代表版本号

        #endregion

        #region attributes

        public string AtHardDiskSequence { get { return HardDiskSequence; } }

        public UInt16 AtCanHardDiskSpace { get { return CanHardDiskSpace; } }

        public int AtCan1State { get { return Can1State; } }

        public int AtCan2State { get { return Can2State; } }

        public int AtMCUState { get { return MCUState; } }

        public int AtDevice485State { get { return Device485State; } }

        public int AtFireboxState { get { return FireboxState; } }

        public int AtFireboxCamState { get { return FireboxCamState; } }

        public UInt32 AtFireboxVedioSpace { get { return FireboxVedioSpace; } }

        public int AtFireboxVersion { get { return FireboxVersion; } }

        #endregion

        #region construction

        public FireboxStatsReport()
        {
            instHeadInfo.instID = 0x0111;
        }

        public FireboxStatsReport(byte[] buf)
        {
            int offset = 0;
            // 获取头信息
            GetHeadInfo(buf, ref offset);
            BodyStreamSplit(buf, ref offset);

        }

        #endregion

        #region function

        public void BodyStreamSplit(byte[] buf ,ref int offset )
        {
            try
            {
                HardDiskSequence = BC.ToBCDString(buf, ref offset, 4);
                CanHardDiskSpace = BC.ToUInt16Net(buf, ref offset);
                Can1State = (int)buf[offset++];
                Can2State = (int)buf[offset++];
                MCUState = (int)buf[offset++];
                Device485State = (int)buf[offset++];
                FireboxState = (int)buf[offset++];
                FireboxCamState = (int)buf[offset++];
                FireboxVedioSpace = BC.ToUInt32Net(buf, offset);
                FireboxVersion = (int)buf[offset++];
            }
            catch (Exception e) { }
        }

        public string CombinedSqlString()
        {
            string _sql = string.Empty;
            _sql = "INSERT INTO table_firebox_basic_stats (TerminalKey,HardDiskSequence," +
               "CanHardDiskSpace,Can1State,Can2State,MCUState,Device485State," +
               "FireboxState,FireboxCamState,FireboxVedioSpace,FireboxVersion) " +
               "VALUES ('" + instHeadInfo.tdPhoneNumber + "','" + HardDiskSequence +
               "'," + CanHardDiskSpace + ',' + Can1State + ',' + Can2State +
               ',' + MCUState + ',' + Device485State + ',' + FireboxState +
               ',' + FireboxCamState + ',' + FireboxVedioSpace + ',' + FireboxVersion +
               ") ON DUPLICATE KEY UPDATE HardDiskSequence=VALUES(HardDiskSequence)," +
               "CanHardDiskSpace=VALUES(CanHardDiskSpace),Can1State=VALUES(Can1State)," +
               "Can2State=VALUES(Can2State),MCUState=VALUES(MCUState)," +
               "Device485State=VALUES(Device485State),FireboxState=VALUES(FireboxState)," +
               "FireboxCamState=VALUES(FireboxCamState),FireboxVedioSpace=VALUES(FireboxVedioSpace)," +
               "FireboxVersion=VALUES(FireboxVersion);";
            return _sql;
        }

        public string InsertString() 
        {
            string _sql = string.Empty;
            _sql="INSERT INTO table_firebox_basic_stats_"+DateTime.Now.ToString("yyyyMMdd")+" (TerminalKey,HardDiskSequence," +
               "CanHardDiskSpace,Can1State,Can2State,MCUState,Device485State," +
               "FireboxState,FireboxCamState,FireboxVedioSpace,FireboxVersion) " +
               "VALUES ('" + instHeadInfo.tdPhoneNumber + "','" + HardDiskSequence +
               "'," + CanHardDiskSpace + ',' + Can1State + ',' + Can2State +
               ',' + MCUState + ',' + Device485State + ',' + FireboxState +
               ',' + FireboxCamState + ',' + FireboxVedioSpace + ',' + FireboxVersion +
               ");";
            _sql += "UPDATE table_abnormal SET can1=" + Can1State + ",can2=" + Can2State  + "',fireboxver="
                + FireboxVersion + ",fbcap=" + CanHardDiskSpace + " WHERE termkey='"
                + instHeadInfo.tdPhoneNumber + "',time='" + DateTime.Now.ToString("yyyy-MM-dd") + ";";
            return _sql;
        }

        #endregion

    }
}
